package com.controller;


import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.UserService;
import com.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChangAn
 * @since 2020-11-27
 */
@RestController
@RequestMapping()
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
     @GetMapping("/user/login")
    public Map<String,Object> login(User user){
         log.info("用户ID：[{}]",user.getId());
         log.info("密码：[{}]",user.getPassword());
         HashMap<String, Object> map = new HashMap<>();
             User user2 = userService.getById(user.getId());
             if (user2!=null&&user2.getPassword().equals(user.getPassword())){
                 HashMap<String, String> payload = new HashMap<>();
                 payload.put("id",user2.getId().toString());
                 payload.put("username",user2.getName());
                 payload.put("password",user2.getPassword());
//                 生成Token信息
                 String token = JwtUtils.getToken(payload);
                 map.put("msg", "认证成功");
                 map.put("token",token);

             } else{
                 throw new RuntimeException("认证失败");
             }
             map.put("status", false);
         return map;
     }

    /**
     * 使用了拦截器之后可以进行业务逻辑的任何操作
     * 这里仅仅是演示一个业务逻辑
     * @param request
     * @return
     */
     @PostMapping("/user/test")
     public Map<String,Object> get(HttpServletRequest request, HttpServletResponse response) throws IOException {
         //获取请求头的认证信息
         String token = request.getHeader("Authorization");
         //获取jwt里面的payloa信息
         DecodedJWT verify = JwtUtils.verify(token);
         String username = verify.getClaim("username").asString();
         String password = verify.getClaim("password").asString();
         HashMap<String, Object> map = new HashMap<>();
         map.put("username", username);
         map.put("password", password);
         log.info("当前token: [{}]",token);
         response.setContentType("application/json;charset=utf-8");
         response.getWriter().println("打印map集合"+map);
         return map;
     }

}
